﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  NotificationPeopleCondition.cs
//    
// Abstract:
//
//  This class represent the people condition for a Notification rule.
//  
//--

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Microsoft.EnhancedReminders
{
    /// <summary>
    /// Sender condition for a notification rule.
    /// </summary>
    [Serializable()]
    public sealed class NotificationPeopleCondition : NotificationCondition
    {
        /// <summary>
        /// Collection of names or aliases. In iteration 2, this can be any strings
        /// </summary>
        Collection<ContactInfo> _contacts = new Collection<ContactInfo>();

        /// <summary>
        /// Constructor.
        /// </summary>
        public NotificationPeopleCondition()
        { 
        }

        /// <summary>
        /// Copy constructor.
        /// </summary>
        public NotificationPeopleCondition(NotificationPeopleCondition peopleCondition)
            : base(peopleCondition)
        {
            Debug.Assert(peopleCondition != null, "peopleCondition");
            Debug.Assert(peopleCondition._contacts != null, "peopleCondition._contacts");

            // Take a deep copy of peopleCondition.Contacts...

            ContactInfo[] contacts = new ContactInfo[peopleCondition._contacts.Count];
            peopleCondition._contacts.CopyTo(contacts, 0);
            _contacts = new Collection<ContactInfo>(new List<ContactInfo>(contacts));
        }

        /// <summary>
        /// Determines if this condition matches the specified data.
        /// </summary>
        /// <param name="notificationData">Data</param>
        /// <returns>
        /// true if the condition matches the sender; false otherwise.
        /// </returns>
        public override bool IsSatisfied(INotificationData notificationData)
        {
            Debug.Assert(notificationData != null, "notificationData != null");

            return Names.Contains(notificationData.Sender);
        }

        /// <summary>
        /// Collection of senders contact info.
        /// </summary>
        public Collection<ContactInfo> Contacts
        {
            get
            {
                Debug.Assert(_contacts != null, "_contacts");

                return _contacts;
            }
        }

        /// <summary>
        /// Collection of senders' display names only.
        /// </summary>
        public Collection<string> Names
        {
            get
            {
                Debug.Assert(_contacts != null, "_contacts");

                Collection<string> names = new Collection<string>();
                
                foreach (ContactInfo contact in _contacts)
                {
                    names.Add(contact.Name);
                }
                
                return names;
            }
        }

        /// <summary>
        /// Collection of senders entry IDs only.
        /// </summary>
        public Collection<string> EntryIds
        {
            get
            {
                Debug.Assert(_contacts != null, "_contacts");

                Collection<string> ids = new Collection<string>();

                foreach (ContactInfo contact in _contacts)
                {
                    ids.Add(contact.EntryId);
                }

                return ids;
            }
        }
    }
}
